package com.s3u4.concurrence;

/**
 * Created by Captain on 28/03/2018.
 */
public class DaemonDemo {


    /**
     * 当java应用内仅存在守护线程时虚拟机会自然退出
     */
    public static class DaemonThread extends Thread {
        @Override
        public void run() {

            /**
             * 如果非守护该线程将一直运行
             */
            while(true){
                System.out.println("HelloWorld");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        DaemonThread thread = new DaemonThread();
        thread.setDaemon(true);
        thread.start();

        Thread.sleep(3000);

        System.out.println("done");
    }

}
